﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace System.Web
{
    public class ZLanguages
    {
        public static Dictionary<string, Dictionary<string, string>> Languages { get; set; }

        public static string GetLanguages(string key)
        {
            if (Languages.ContainsKey(GetLanguagesType()) && Languages[GetLanguagesType()].ContainsKey(key))
                return Languages[GetLanguagesType()][key];
            else return key;
        }

        public static string GetLanguagesType()
        {
            return "中文";
        }
        public static bool IFUseLanguage
        {
            get
            {
                return ZConfig.IFUseLanguage;
            }
        }

        public static void InitLanguages()
        {
            Languages = new Dictionary<string, Dictionary<string, string>>();
            string dir = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Languages");
            if (Directory.Exists(dir))
            {
                string[] files = Directory.GetFileSystemEntries(dir);
                foreach (string file in files)
                {
                    Languages[Path.GetFileNameWithoutExtension(file)] = ReadToDictionary(file);
                }
            }
        }

        public static Dictionary<string, string> ReadToDictionary(string path)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            if (File.Exists(path))
            {
                string line = "";
                using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
                {
                    while (!string.IsNullOrEmpty(line = sr.ReadLine()))
                    {
                        string[] temp = line.Split(':');
                        if (temp.Length == 2)
                            result[temp[0]] = temp[1];
                    }
                }
            }
            return result;
        }
    }
}
